<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
<#include "../common/header.ftl">
    <script src="/js/plugins/chosen/chosen.jquery.js"></script>
    <link rel="stylesheet" href="/js/plugins/chosen/component-chosen.css"/>
    <script src="/js/plugins/ckeditor/ckeditor.js"></script>
    <script>
        $(function () {
            $('#pagination').twbsPagination({
                totalPages: ${result.pages} || 1,
                startPage: ${result.pageNum},
                visiblePages: ${result.pageSize},
                first: "首页", prev: "上一页",
                next: "下一页",
                last: "末页",
                onPageClick: function (event, page) {
                    $("#currentPage").val(page);
                    $("#searchForm").submit();
                }
            });
            //初始化ck编辑器
            var ckeditor = CKEDITOR.replace('editor', {
                height: 300
            });
            //获取所有日报类型
            $.get('/news/typeList.do',function (data) {
                var html = "";
                $.each(data,function (index,ele) {
                    html += "<option value='" + ele.id + "'>" + ele.name + "</option>";
                })
                $("#type").html(html);
            })
            //添加按钮
            $("#addBtn").click(function () {
                $("#editForm")[0].reset();
                $("#coverImg").attr('width', 0);
                $("#newsModal").modal("show");
            });
            //修改按钮
            $(".editBtn").click(function () {
                $("#editForm")[0].reset();
                var json = $(this).data("json");
                $("#newsId").val(json.id);
                $("#title").val(json.title);
                $("#abstracts").val(json.abstracts);
                //封面
                $("#coverImg").attr('width', 100);
                $("#coverImg").attr('src', json.coverUrl);
                $("#coverUrl").val(json.coverUrl);
                //回显表单中的日报类型下拉框
                $("#type option[value=" + json.typeId + "]").prop('selected', true);
                //回显文章内容
                $.get('/news/getContentById.do', {id: json.id}, function (data) {
                    ckeditor.setData(data.content);
                })
                $("#newsModal").modal("show");
            })
            //发布功能
            $(".changeStateBtn").click(function () {
                //获取游记的id
                var id = $(this).data("id");
                //获取要修改的状态
                var state = $(this).data("state");
                //更改状态
                $.get("/news/changeState.do", {id: id, state: state}, function (data) {
                    showMessager(data);
                })
            });
            //删除功能
            $(".deleteBtn").click(function () {
                //获取游记的id
                var id = $(this).data("id");
                //删除日报
                $.get('/news/delete.do', {id: id}, function (data) {
                    showMessager(data);
                })
            });
            //保存按钮
            $("#saveBtn").click(function () {
                //保存时需要通过getData方法将富文本编辑器的内容设置到textarea中
                var data = ckeditor.getData();
                $("#editor").val(data);
                $("#editForm").ajaxSubmit(function (data) {
                    showMessager(data);
                })
            })
        })
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
<#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
        <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">待审核日报管理</h1>
                </div>
            </div>
            <!--高级查询-->
            <form class="form-inline" id="searchForm" action="/news/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label for="keyword">关键字:</label>
                    <input type="text" class="form-control" id="keyword" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题">
                    <button id="btn_query" class="btn btn-success">查询</button>
                    <button type="button" id="addBtn" class="btn btn-primary">添加骡窝日报</button>
                </div>
            </form>
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>创建时间</th>
                    <th>类型</th>
                </tr>
                </thead>
            <#list result.list as entity>
                <tr>
                    <td>${entity_index+1}</td>
                    <td>${entity.title}</td>
                    <td><img src="${entity.coverUrl}" width="30px"></td>
                    <td>${(entity.createTime)?string('yyyy-MM-dd')}</td>
                    <td>${entity.type.name}</td>
                    <td>
                        <a href="javascript:;" class="changeStateBtn" data-state="1" data-id='${entity.id}'>发布</a>
                    </td>
                    <td>
                        <a href="javascript:;" class="editBtn" data-json='${entity.jsonString}'>修改</a>
                    </td>
                    <td>
                        <a href="javascript:;" class="deleteBtn" data-id="${entity.id}">删除</a>
                    </td>
                </tr>
            </#list>
            </table>
            <div style="text-align: center;">
                <ul id="pagination" class="pagination"></ul>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="newsModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content modal-lg">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">骡窝日报新增/编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/news/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" id="newsId" name="id">
                    <input type="hidden" id="state" name="state" value="0">
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="title" name="title">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">摘要：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="abstracts" name="abstracts">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img id="coverImg" width="50%">
                            <input type="hidden" class="form-control" id="coverUrl" name="coverUrl">
                            <input type="file" class="form-control" id="coverFile" name="file">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">日报类型：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="type.id" id="type">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea id="editor" name="details.content" rows="10" cols="80">
                        </textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-success" id="saveBtn">保存</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>